#include "stdafx.h"
#include "FileSystem.h"

void (*DoneCallback)(CXuiElement);

void updateDrives(CXuiList list, CXuiTextElement text) {
	text.SetText(L"");
	list.DeleteItems(0, list.GetItemCount());
	DWORD currentButton = 0;

	vector<Drive*> drives = FileSystem::getInstance().GetDrives();
	for(DWORD i = 0;i < drives.size();i++) {
		if(drives[i]->IsMounted()) {
			list.InsertItems(currentButton, 1);
			LPWSTR wstr = new wchar_t[strlen(drives[i]->GetDriveName()) + 3];
			ZeroMemory(wstr, strlen(drives[i]->GetDriveName()) + 3);
			MultiByteToWideChar(CP_UTF8, 0, drives[i]->GetDriveName(), -1, wstr, strlen(drives[i]->GetDriveName()) + 1);

			wstr[strlen(drives[i]->GetDriveName())] = ':';
			wstr[strlen(drives[i]->GetDriveName()) + 1] = '\\';
			wstr[strlen(drives[i]->GetDriveName()) + 2] = 0;

			list.SetText(currentButton, wstr);
			currentButton++;

			delete wstr;
		}
	}
}

void ChangeDir(CXuiList list, const char* path, CXuiTextElement xuiText) {
	list.DeleteItems(0, list.GetItemCount());
	
	// List everything in the drive
	string sPath = path;
	sPath += "*";
	WIN32_FIND_DATA wfd;
	HANDLE hFind;
	wchar_t* wtext = new wchar_t[1024];
	hFind = FindFirstFile(sPath.c_str(), &wfd);
	if(hFind != INVALID_HANDLE_VALUE)
		do {
			if(wfd.dwFileAttributes & 0x10) { // folder
				list.InsertItems(list.GetItemCount(), 1);
			
				MultiByteToWideChar(CP_UTF8, NULL, wfd.cFileName, -1, wtext, 1024);

				list.SetText(list.GetItemCount() - 1, wtext);
			}
		} while(FindNextFile(hFind, &wfd));
	FindClose(hFind);

	MultiByteToWideChar(CP_UTF8, NULL, path, -1, wtext, 1024);

	xuiText.SetText(wtext);

	delete wtext;
}

HRESULT CChangeDirectoryScene::OnInit(XUIMessageInit* pInitData, BOOL &bHandled) {
	GetChildById(L"XuiTextPath", &xuiTextPath);
	GetChildById(L"XuiCommonListPaths", &xuiListPaths);
	GetChildById(L"XuiButtonSetDir", &xuiButtonSetDir);
	GetChildById(L"XuiBackButton1", &xuiButtonBack);

	xuiListPaths.InitFocus();

	updateDrives(xuiListPaths, xuiTextPath);

	return S_OK;
}

HRESULT CChangeDirectoryScene::OnNotifyPress(HXUIOBJ hObjPressed, BOOL &bHandled) {
	
	if(hObjPressed == xuiListPaths) {
		DWORD listIndex;

		listIndex = xuiListPaths.GetCurSel();
		
		LPCWSTR drivePathW = xuiListPaths.GetText(listIndex);
		int length = wcslen(drivePathW);
		char *drivePathTemp = new char[1024];

		LPCWSTR xuiTextText = xuiTextPath.GetText();

		string drivePath = "";

		if(xuiTextText && wcslen(xuiTextText) > 0) {
			ZeroMemory(drivePathTemp, 1024);
			WideCharToMultiByte(CP_UTF8, NULL, xuiTextPath.GetText(), -1, drivePathTemp, 1024, NULL, NULL);
			drivePath += drivePathTemp;
		}

		ZeroMemory(drivePathTemp, 1024);
		WideCharToMultiByte(CP_UTF8, NULL, drivePathW, -1, drivePathTemp, 1024, NULL, NULL);

		drivePath += drivePathTemp;
		if(drivePath[drivePath.size() - 1] != '\\')
			drivePath += '\\';

		delete drivePathTemp;

		ChangeDir(xuiListPaths, drivePath.c_str(), xuiTextPath);
	}
	else if(hObjPressed == xuiButtonSetDir) {
		if(DoneCallback != 0)
			DoneCallback(xuiTextPath);
		NavigateBack();
	}
	else if(hObjPressed == xuiButtonBack) {
		if(xuiTextPath.GetText() != 0) {
			char* text = new char[1024];
			WideCharToMultiByte(CP_UTF8, NULL, xuiTextPath.GetText(), -1, text, 1024, NULL, NULL);
			string path(text);
			delete text;

			int pos = path.find_last_of("\\");
			if(pos == path.find("\\")) {
				updateDrives(xuiListPaths, xuiTextPath);
			}
			else {	
				pos = path.find_last_of("\\", pos - 1) + 1;
				path = path.substr(0, pos);
				ChangeDir(xuiListPaths, path.c_str(), xuiTextPath);
			}
		}
		else
			NavigateBack();
	}

	return S_OK;
}

void DirSceneRegisterCallback(void (*callback)(CXuiElement)) {
	DoneCallback = callback;
}